package com.diaryBlogModule

class DiaryEntityController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }
    def diaryList={
      def diaryBook=Diary.get(params.id)
      params.max = Math.min(params.max ? params.int('max') : 10, 100)
      println DiaryEntity.countByDiary(diaryBook)+">>>>>>>>>"
      [diaryEntityInstanceList: DiaryEntity.findAllByDiary(diaryBook), diaryEntityInstanceTotal: DiaryEntity.countByDiary(diaryBook)]
    }
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [diaryEntityInstanceList: DiaryEntity.list(params), diaryEntityInstanceTotal: DiaryEntity.count()]
    }

    def create = {
        def diaryEntityInstance = new DiaryEntity()
        diaryEntityInstance.properties = params
        return [diaryEntityInstance: diaryEntityInstance]
    }

    def save = {
        def diaryEntityInstance = new DiaryEntity(params)
      Calendar cal=Calendar.getInstance()
      diaryEntityInstance.setYear (cal.get(Calendar.YEAR))
      diaryEntityInstance.setMonth (cal.get(Calendar.MONTH))
      diaryEntityInstance.setDay (cal.get(Calendar.DAY_OF_MONTH))
        if (diaryEntityInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'diaryEntity.label', default: 'DiaryEntity'), diaryEntityInstance.id])}"
            redirect(action: "show", id: diaryEntityInstance.id)
        }
        else {
            render(view: "create", model: [diaryEntityInstance: diaryEntityInstance])
        }
    }
    def showDiary={
      def diary=Diary.get(params.id)
      [diaryInstance:diary]
    }
    def show = {
        def diaryEntityInstance = DiaryEntity.get(params.id)
        if (!diaryEntityInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'diaryEntity.label', default: 'DiaryEntity'), params.id])}"
            redirect(action: "list")
        }
        else {
            [diaryEntityInstance: diaryEntityInstance]
        }
    }

    def edit = {
        def diaryEntityInstance = DiaryEntity.get(params.id)
        if (!diaryEntityInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'diaryEntity.label', default: 'DiaryEntity'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [diaryEntityInstance: diaryEntityInstance]
        }
    }

    def update = {
        def diaryEntityInstance = DiaryEntity.get(params.id)
        if (diaryEntityInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (diaryEntityInstance.version > version) {
                    
                    diaryEntityInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'diaryEntity.label', default: 'DiaryEntity')] as Object[], "Another user has updated this DiaryEntity while you were editing")
                    render(view: "edit", model: [diaryEntityInstance: diaryEntityInstance])
                    return
                }
            }
            diaryEntityInstance.properties = params
            if (!diaryEntityInstance.hasErrors() && diaryEntityInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'diaryEntity.label', default: 'DiaryEntity'), diaryEntityInstance.id])}"
                redirect(action: "show", id: diaryEntityInstance.id)
            }
            else {
                render(view: "edit", model: [diaryEntityInstance: diaryEntityInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'diaryEntity.label', default: 'DiaryEntity'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def diaryEntityInstance = DiaryEntity.get(params.id)
        if (diaryEntityInstance) {
            try {
                diaryEntityInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'diaryEntity.label', default: 'DiaryEntity'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'diaryEntity.label', default: 'DiaryEntity'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'diaryEntity.label', default: 'DiaryEntity'), params.id])}"
            redirect(action: "list")
        }
    }
}
